package com.zoran.study.spring.controller.interceptors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * MyFirstInterceptor
 *
 * @author zouxm
 * @date 16/12/29
 */
//spring mvc 拦截器定义
@Slf4j
public class MyFirstInterceptor implements HandlerInterceptor {

    // 在目标方法之前调用,如果返回false,则不会调用目标方法,也不会调用后续的拦截器
    // 如果此方法返回true,则对应的afterCompletion一定会被调用,返回false不会调用
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("MyFirstInterceptor preHandle");
        /*response.getWriter().write("not allow");
        return false;*/
        return true;
    }

    // 在目标方法之后调用,但是在渲染视图之前调用
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyFirstInterceptor postHandle");
    }

    //在渲染视图之后调用,释放资源等
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyFirstInterceptor afterCompletion");
    }
}
